package com.jxs.android.ges;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.weiget.BuLoadingDailog;

@SuppressLint("HandlerLeak")
public class LaucherMyInfo extends AbstractActivity implements LaucherMyInfoManager.UI {

	private BuLoadingDailog progressDialog;
	private LaucherMyInfoManager.UI.ViewHolder viewHolder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bu_my_info_view);
		viewHolder = new LaucherMyInfoManager.UI.InitViewHolder(this).init();
		loadingMyInfo();
	}

	private void loadingMyInfo() {
		progressDialog = new BuLoadingDailog(this);
		progressDialog.showLoading(R.string.load_and_wait);
		new Thread(new Runnable() {
			@Override
			public void run() {
				initMyInfo(GesApplication.getUserInfo().getTicket());
			}
		}).start();
	}

	private void initMyInfo(String ticket) {
		String wsdl_url = GesApplication.WSDL_URL;// wsdl
		String namespace = GesApplication.WSDL_NAME_SPACE;// namespace
		String methodName = "getStaffInfo";// 要调用的方法名称
		SoapObject request = new SoapObject(namespace, methodName);
		request.addProperty("ticket", ticket);
		BuWebService webService = new BuWebService(wsdl_url, request);
		webService.setDoNet(false);
		webService.requestWSDL(new IWebService() {
			@Override
			public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
				SoapObject response = (SoapObject) envelope.getResponse();
				paser(response);
			}

			@Override
			public void error(String message, Exception e) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			}

		});
	}

	private void paser(SoapObject soapObject) {
		if (null != soapObject) {
			String stauts = soapObject.getProperty("status").toString();
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getProperty("message").toString();
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {

				SoapObject taskBeanObject = (SoapObject) soapObject.getProperty("retMsg");
				Message msg = new Message();
				msg.what = UPDATE_LIST_UI;
				msg.obj = paserData(taskBeanObject);
				handler.sendMessage(msg);
			}
		}
	}

	private MyInfo paserData(SoapObject taskBeanObject) {
		MyInfo cstInfo = new MyInfo();
		if (null != taskBeanObject) {
			cstInfo.my_info_staff_no = taskBeanObject.getProperty("staff_no").toString();// 客户类型
			cstInfo.my_info_name = taskBeanObject.getProperty("name").toString();// 客户ARPU
			cstInfo.my_info_phone = taskBeanObject.getProperty("cdma_no").toString();// cdma_no
			cstInfo.my_info_latn_name = taskBeanObject.getProperty("latn_name").toString();// 产品信息
			cstInfo.my_info_role_name = taskBeanObject.getProperty("role_name").toString();// 收入信息
		}
		return cstInfo;
	}

	private class MyInfo {
		String my_info_staff_no = "";// 工号
		String my_info_name = "";// 姓名
		String my_info_phone = "";
		String my_info_latn_name = "";// 分公司
		String my_info_role_name = "";// 角色
	}

	private final int UPDATE_LIST_UI = 0x101;
	private final int ERROR_MSG = 0x1013;
	private final int TO_LOGIN = 0x1019;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if (msg.what == UPDATE_LIST_UI) {
				updateList(msg);
			} else if (msg.what == ERROR_MSG) {
				errorMsgPop(msg.obj.toString());
			} else if (msg.what == TO_LOGIN) {
				errorMsgToLogin(msg.obj.toString());
			}
		}
	};

	private void updateList(Message msg) {
		MyInfo info = (MyInfo) msg.obj;

		viewHolder.my_info_staff_no.setText(info.my_info_staff_no);// 工号
		viewHolder.my_info_name.setText(info.my_info_name);// 姓名
		viewHolder.my_info_phone.setText(info.my_info_phone);// 姓名
		viewHolder.my_info_latn_name.setText(info.my_info_latn_name);// 分公司
		viewHolder.my_info_role_name.setText(info.my_info_role_name);// 角色

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return onKeyDownListener(keyCode, event);
	}

}
